package com.xz.courseschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.courseschedule.common.vo.BaseVo;
import com.xz.courseschedule.entity.Course;
import com.xz.courseschedule.mapper.CourseMapper;
import com.xz.courseschedule.service.CourseService;
import com.xz.courseschedule.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: zys
 * @CreateTime: 2025-05-24
 */

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Override
    public Result getList() {
        List<Course> courseList = super.list();
        return Result.success("获取课程表成功",courseList);
    }

    @Override
    public Result getById(Long id) {
        if (id == null) {
            return Result.error("课程ID不能为空");
        }
        Course course = super.getById(id);
        if (course == null) {
            return Result.error("课程不存在");
        }
        return Result.success("获取课程成功", course);
    }

    @Override
    public Result add(Course course) {
        if (course == null) {
            return Result.error("课程信息不能为空");
        }
        if (course.getCourseNo() == null || course.getCourseNo().trim().isEmpty()) {
            return Result.error("课程编号不能为空");
        }
        if (course.getCourseName() == null || course.getCourseName().trim().isEmpty()) {
            return Result.error("课程名称不能为空");
        }

        // 检查课程编号是否已存在
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<Course>()
                .eq(Course::getCourseNo, course.getCourseNo());
        Course existingCourse = super.getOne(wrapper);
        if (existingCourse != null) {
            return Result.error("课程编号已存在");
        }

        boolean result = super.save(course);
        if (result) {
            return Result.success("添加课程成功", course);
        } else {
            return Result.error("添加课程失败");
        }
    }

    @Override
    public Result update(Course course) {
        if (course == null || course.getId() == null) {
            return Result.error("课程信息不完整");
        }

        Course existingCourse = super.getById(course.getId());
        if (existingCourse == null) {
            return Result.error("课程不存在");
        }

        // 如果修改了课程编号，检查新编号是否已存在
        if (course.getCourseNo() != null && !course.getCourseNo().equals(existingCourse.getCourseNo())) {
            LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<Course>()
                    .eq(Course::getCourseNo, course.getCourseNo())
                    .ne(Course::getId, course.getId());
            Course duplicateCourse = super.getOne(wrapper);
            if (duplicateCourse != null) {
                return Result.error("课程编号已存在");
            }
        }

        boolean result = super.updateById(course);
        if (result) {
            return Result.success("更新课程成功", course);
        } else {
            return Result.error("更新课程失败");
        }
    }

    @Override
    public Result delete(Long id) {
        if (id == null) {
            return Result.error("课程ID不能为空");
        }

        // 物理删除
        boolean result = super.removeById(id);
        if (result) {
            return Result.success("删除课程成功");
        } else {
            return Result.error("删除课程失败");
        }
    }

    @Override
    public Result deleteBatch(Long[] ids) {
        if (ids == null) {
            return Result.error("课程ID列表不能为空");
        }

        // 物理删除
        boolean result = super.removeByIds(Arrays.asList(ids));
        if (result) {
            return Result.success("批量删除课程成功");
        } else {
            return Result.error("批量删除课程失败");
        }
    }
}
